package zpin.action;

import java.util.HashSet;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import zpin.dao.AuthorizationDao;
import zpin.dao.CityDao;
import zpin.dao.CompanyDao;
import zpin.dao.EngageDao;
import zpin.dao.PersonDao;
import zpin.dao.ResumeDao;
import zpin.dao.RoleDao;
import zpin.dao.TypeDao;
import zpin.model.Authorization;
import zpin.model.Company;
import zpin.model.Person;
import zpin.model.Role;

import com.opensymphony.xwork2.ActionSupport;

public class PersonAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Long id;

	private Person person;

	private Company company;

	private Authorization authorization;

	private PersonDao personDao;

	private CompanyDao companyDao;

	private AuthorizationDao authorizationDao;

	private CityDao cityDao;

	private EngageDao engageDao;

	private ResumeDao resumeDao;

	private RoleDao roleDao;

	private TypeDao typeDao;

	private String password;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	public EngageDao getEngageDao() {
		return engageDao;
	}

	public void setEngageDao(EngageDao engageDao) {
		this.engageDao = engageDao;
	}

	public ResumeDao getResumeDao() {
		return resumeDao;
	}

	public void setResumeDao(ResumeDao resumeDao) {
		this.resumeDao = resumeDao;
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	public TypeDao getTypeDao() {
		return typeDao;
	}

	public void setTypeDao(TypeDao typeDao) {
		this.typeDao = typeDao;
	}

	public Person getPerson() {
		return person;
	}

	public void setPerson(Person person) {
		this.person = person;
	}

	public Authorization getAuthorization() {
		return authorization;
	}

	public void setAuthorization(Authorization authorization) {
		this.authorization = authorization;
	}

	public PersonDao getPersonDao() {
		return personDao;
	}

	public void setPersonDao(PersonDao personDao) {
		this.personDao = personDao;
	}

	public AuthorizationDao getAuthorizationDao() {
		return authorizationDao;
	}

	public void setAuthorizationDao(AuthorizationDao authorizationDao) {
		this.authorizationDao = authorizationDao;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public CompanyDao getCompanyDao() {
		return companyDao;
	}

	public void setCompanyDao(CompanyDao companyDao) {
		this.companyDao = companyDao;
	}

	public String start() {
		return SUCCESS;
	}

	public String personLogin() {
		System.out.println(authorization.getPassword());
		System.out.println(password);
		if (company != null && authorization != null
				&& !authorization.getPassword().equals(password)) {
			System.out.println("两次密码不相等");
			addFieldError("errorlabel", "两次密码不相等");
			return "error";
		}
		HttpSession session = ServletActionContext.getRequest().getSession();
		System.out.println("开始注册");
		if (person != null) {
			if (!authorizationDao.matchAccount(authorization)) {
				Set<Role> roles = new HashSet<Role>();
				person.setAuthorization(authorization);
				roles.add(roleDao.findById(1L));
				authorization.setRoles(roles);
				authorizationDao.saveOrUpdate(authorization);
				personDao.saveOrUpdate(person);

				session.setAttribute("authorization", personDao
						.findByFk(authorizationDao.match(authorization)));
				System.out.println("authorization的session为："
						+ session.getAttribute("authorization"));
				return SUCCESS;
			} else {
				addFieldError("errorlabel", "用户名已存在");
				return "error";
			}
		} else {
			addFieldError("errorlabel", "用户名已存在");
			return "error";
		}

	}

	public String companyLogin() {
		if (person != null && authorization != null
				&& !authorization.getPassword().equals(password)) {
			addFieldError("errorlabel", "两次密码不相等");
			return "error";
		}
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (company != null) {
			if (!authorizationDao.matchAccount(authorization)) {
				Set<Role> roles = new HashSet<Role>();
				company.setAuthorization(authorization);
				roles.add(roleDao.findById(2L));
				authorization.setRoles(roles);
				authorizationDao.saveOrUpdate(authorization);
				companyDao.saveOrUpdate(company);

				session.setAttribute("authorization", companyDao
						.findByFk(authorizationDao.match(authorization)));
				System.out.println("authorization的session为："
						+ session.getAttribute("authorization"));
			} else {
				addFieldError("errorlabel", "用户名已存在");
				return "error";
			}
		} else {
			addFieldError("errorlabel", "用户名已存在");
			return "error";
		}
		return SUCCESS;
	}

	public String toPersonLogin() {
		return SUCCESS;
	}

	public String toCompanyLogin() {
		return SUCCESS;
	}

	public String personLoad() {
		System.out.println("开始登陆");
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (authorizationDao.match(authorization) != null) {
			if (authorizationDao.matchPerson(authorization)) {
				session.setAttribute("authorization", personDao
						.findByFk(authorizationDao.match(authorization)));
				System.out.println("authorization的session为："
						+ session.getAttribute("authorization"));
				return SUCCESS;
			} else if (authorizationDao.matchAdmin(authorization)) {
				session.setAttribute("authorization", "admin");
				System.out.println("authorization的session为："
						+ session.getAttribute("authorization"));
				return "admin";
			} else {
				this.addFieldError("errorlabel", "账号密码错误");
				return "error";
			}
		} else {
			this.addFieldError("errorlabel", "账号密码错误");
			return "error";
		}
	}

	public String companyLoad() {
		System.out.println("开始登陆");
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (authorizationDao.match(authorization) != null) {
			if (authorizationDao.matchCompany(authorization)) {
				session.setAttribute("authorization", companyDao
						.findByFk(authorizationDao.match(authorization)));
				System.out.println("authorization的session为："
						+ session.getAttribute("authorization"));
				return SUCCESS;
			} else {
				this.addFieldError("errorlabel", "账号密码错误");
				return "error";
			}
		} else {
			this.addFieldError("errorlabel", "账号密码错误");
			return "error";
		}
	}

	public String toPersonLoad() {
		return SUCCESS;
	}

	public String toCompanyLoad() {
		return SUCCESS;
	}

	public String function() {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if (Person.class.isInstance(session.getAttribute("authorization"))) {
			return "person";
		}

		if (Company.class.isInstance(session.getAttribute("authorization"))) {
			return "company";
		}

		if (String.class.isInstance(session.getAttribute("authorization"))) {
			return "admin";
		}

		return SUCCESS;

	}

	public String exit() {
		System.out.println("开始退出");
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute("authorization");
		return SUCCESS;
	}

	public String update() {
		Person temp = new Person();
		temp = (Person) ServletActionContext.getRequest().getSession()
				.getAttribute("authorization");
		person.setAuthorization(temp.getAuthorization());
		personDao.update(person);
		return SUCCESS;
	}

	public String select() {
		Person temp = new Person();
		temp = (Person) ServletActionContext.getRequest().getSession()
				.getAttribute("authorization");
		person = personDao.findById(temp.getId());
		return SUCCESS;
	}

	public String change() {
		person = (Person) ServletActionContext.getRequest().getSession()
				.getAttribute("authorization");
		return SUCCESS;
	}

	public String input() {
		return "input";
	}
}
